<?php

/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\cashier\model\order;


use addon\memberconsume\model\Consume;
use addon\store\model\settlement\OrderSettlement;
use app\model\order\OrderCommon;
use app\model\order\OrderLog;

/**
 * 订单创建(收银订单)
 *
 * @author Administrator
 *
 */
class CashierOrder extends OrderCommon
{
    //待付款
    const ORDER_CREATE = 0;
    // 订单已支付
    const ORDER_PAY = 1;
    // 订单已完成
    const ORDER_COMPLETE = 10;
    // 订单已关闭
    const ORDER_CLOSE = -1;

    // 订单类型
    public $order_type = 5;

    // 订单来源
    public $order_from_list = [ 'cashier' => [ 'name' => '收银台' ] ];
    public $order_status = [
        self::ORDER_CREATE => [
            'status' => self::ORDER_CREATE,
            'name' => '待支付',
            'is_allow_refund' => 0,
            'icon' => 'public/uniapp/order/order-icon.png',
            'action' => [
                [
                    'action' => 'orderClose',
                    'title' => '关闭订单',
                    'color' => ''
                ]
            ],
            'member_action' => [
                [
                    'action' => 'orderClose',
                    'title' => '关闭订单',
                    'color' => ''
                ],

            ],
            'color' => ''
        ],
        self::ORDER_COMPLETE => [
            'status' => self::ORDER_COMPLETE,
            'name' => '已完成',
            'is_allow_refund' => 1,
            'icon' => 'public/uniapp/order/order-icon-received.png',
            'action' => [
            ],
            'member_action' => [

            ],
            'color' => ''
        ],
        self::ORDER_CLOSE => [
            'status' => self::ORDER_CLOSE,
            'name' => '已关闭',
            'is_allow_refund' => 0,
            'icon' => 'public/uniapp/order/order-icon-close.png',
            'action' => [

            ],
            'member_action' => [

            ],
            'color' => ''
        ],
    ];

    public $pay_type = array (
        'cash' => '现金支付',
        'BALANCE' => '余额支付',
        'own_wechatpay' => '个人微信',
        'own_alipay' => '个人支付宝',
        'own_pos' => '个人pos刷卡',
        'ONLINE_PAY' => '在线支付',
    );

    public $cashier_order_type = [
        'goods' => '消费',
        'card' => '售卡',
        'recharge' => '充值',
    ];

    //todo  切勿调用,占位
    public $delivery_status_list = array (
        0 => '待发货',
        1 => '已发货',
        2 => '已收货'
    );

    public function getPayType($params = [])
    {
        return $this->pay_type;
    }

    public function getCashierOrderType()
    {
        return $this->cashier_order_type;
    }

    public function orderPay()
    {

    }

    public function refund($params)
    {
        return $this->success();
    }


    public function orderDetail($order_info)
    {
        return [];
    }

    /**
     * 订单完成
     * @param $params
     * @return array
     */
    public function complete($params)
    {
        $site_id = $params[ 'site_id' ];
        $order_id = $params[ 'order_id' ];
        $cashier_order_model = new CashierOrder();
        $cashier_data = array (
            'order_status' => 10,
            'finish_time' => time(),
            'order_status_name' => $cashier_order_model->order_status[ 10 ][ 'name' ],
            'order_status_action' => json_encode($cashier_order_model->order_status[ 10 ], JSON_UNESCAPED_UNICODE)
        );
        $cashier_condition = array (
            [ 'order_id', '=', $order_id ],
        );
        model('order')->update($cashier_data, $cashier_condition);

        //收银台的订单因部分不可抗力不能进行公共的订单完成事件,所以主动调用门店订单分成结算
        if (addon_is_exit('store')) {
            ( new  OrderSettlement() )->orderSettlementAccount([ 'order_id' => $order_id ]);
        }

        //模拟订单支付后操作钩子
        if (addon_is_exit('memberconsume')) {
            $consume_model = new Consume();
            $consume_model->memberConsume([ 'out_trade_no' => $params[ 'out_trade_no' ], 'status' => 'complete' ], $order_id);

        }
        $log_data = array (
            'order_id' => $order_id,
            'action' => 'complete',
            'site_id' => $site_id,
            'is_auto' => 1
        );
        ( new OrderLog() )->addLog($log_data);
        return $this->success();

    }
}
